package com.hdu.hot100.list;

import java.util.List;

public class lc234 {


    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    class Solution {
        public boolean isPalindrome(ListNode head)
        {
            ListNode half = findHalf(head);
            ListNode next = half.next;
            half.next = next;
            ListNode newNode = reverseList(next);
            while (newNode != null)
            {
                if (newNode.val != head.val)
                {
                    return false;
                }
                newNode = newNode.next;
                head = head.next;
            }
            return true;
        }


        ListNode reverseList(ListNode head)
        {
            if (head == null || head.next == null)
            {
                return head;
            }
            ListNode cur = reverseList(head.next);
            head.next.next = head;
            head.next = null;
            return cur;
        }

        ListNode findHalf(ListNode head)
        {
            ListNode slow = head;
            ListNode fast = head;
            while (fast != null && fast.next != null)
            {
                fast = fast.next.next;
                if (fast == null) return slow;
                slow = slow.next;
            }
            return slow;
        }
    }
}
